#!/usr/bin/env node
const log = console.log;
var buf1 = new Buffer(256);
buf1[0] = 0;
log('buffer1 length:',buf1.length);
log('\nbuffer1 content:',buf1);
//通过循环初始化，每个字节比上一个字节大1
for(let i = 0;i<256;i++){
  buf1[i]=i;
}
log('\nbuf1:',buf1);

//切片
var buf2 = buf1.slice(250,256);
log('\nbuf2 length:',buf2.length);
log('\nbuf2 content:',buf2);

//对buf2进行填充操作，所有字节填充为零
buf2.fill(0);
log('\nbuf2 end',buf2);
//buffer数据转化成JSON数据
log('\nbuf2\'s JSON:',buf2.toJSON());
log('\nbuf2\'s JSON:',JSON.stringify(buf2));

//数组实例化buffer
let arr = ['a',0xba,0xdf,0x00,255,10];
let buf3 = Buffer.from(arr);
log('\nbuf3 length:',buf3.length);
log('\nbuf3 content:',buf3);
//用字符串实例化buffer
let buf4 = Buffer.from('hello','utf8');
log('\nbuf4 length:',buf4.length);
log('\nbuf4 content:',buf4);

//buffer数据复制
buf4.copy(buf3,0,0,buf3.length);
log('\nbuf3 lenghth:',buf3.length);
log('\nbuf3 content:',buf3);
//bianma
let str = 'ni好';
let buf5 = Buffer.from(str,'utf8');
log('\n buf5 length:',buf5.length);
log('\n buf5 content:',buf5);
log('\nstring length:',str.length);

